jconed.toolbar.register_handler ( 'Table', function ( editor )
{
	var f = new liwe.form.instance ( 'jconed-table', 'no-action' );
	f.number ( { label: 'Rows', name: 'rows', size: 5 } );
	f.number ( { label: 'Columns', name: 'cols', size: 5 } );
	f.submit ( 'Insert' );
	f.events [ 'submit' ] = function ()
	{
		var table = document.createElement ( 'table' );
		var rows = f.get_value ( 'rows' );
		var cols = f.get_value ( 'cols' );
		for ( var x = 0; x < rows; x++ )
		{
			var row = document.createElement ( 'tr' );
			for ( var y = 0; y < cols; y++ )
			{
				var cell = document.createElement ( 'td' );
				cell.appendChild ( document.createElement ( 'br' ) );
				row.appendChild ( cell );
			}
			table.appendChild ( row );
		}
		jconed.execute ( 'insertNode', table );
		return false;
	};
	jconed.toolbar.set_context ( f.get () );
} );

jconed.__table_get_selection = function ()
{
	var editor = jconed.editor.current;
	var editor_el = $( editor.id );
	var selections = jconed.editor.get_selection ();
	var sel = selections [ 0 ];
	var item;
	if ( selections.length > 1 )
		var item = sel.startContainer.childNodes [ sel.startOffset ];
	else
		var item = sel.startContainer;

	while ( item && item.tagName != 'TD' && item.tagName != 'TH' && item != editor_el )
		item = item.parentNode;
	var td = item;
	
	while ( item && item.tagName != 'TABLE' && item != editor_el )
		item = item.parentNode;
	var table = item;
	
	if ( ! td || ! table || table.tagName != 'TABLE' ) return false;

	return [ td, table ];
}

jconed.register_context_handler ( function ( editor )
{
	var sel = jconed.__table_get_selection ();
	if ( ! sel ) return;
	var td = sel [ 0 ], table = sel [ 1 ];
	var selections = jconed.editor.get_selection ();

	var table_border_size = parseInt ( table.style.borderWidth, 10 );
	if ( isNaN ( table_border_size ) )
		table_border_size = '';

	var border_size = parseInt ( td.style.borderWidth, 10 );
	if ( isNaN ( border_size ) )
		border_size = '';

	var table_padding = table.cellPadding;
	var table_spacing = table.cellSpacing;
	var table_border_size = table_border_size;
	var table_border_color = jconed.rgb_to_hex ( table.style.borderColor );
	var border_size = border_size;
	var border_color = jconed.rgb_to_hex ( td.style.borderColor );

	var buttons = '' +
		'<div class="jconed-button jconed-border-left" onclick="jconed.__table_context(\'%(table-left)s\', \'%(sel-left)s\')"></div>' +
		'<div class="jconed-button jconed-border-right" onclick="jconed.__table_context(\'%(table-right)s\', \'%(sel-right)s\')"></div>' +
		'<div class="jconed-button jconed-border-top" onclick="jconed.__table_context(\'%(table-top)s\', \'%(sel-top)s\')"></div>' +
		'<div class="jconed-button jconed-border-bottom" onclick="jconed.__table_context(\'%(table-bottom)s\', \'%(sel-bottom)s\')"></div>' +
		'<div class="jconed-button jconed-border-all" onclick="jconed.__table_context(\'%(table-all)s\', \'%(sel-all)s\')"></div>';

	var button_table = {
		'table-left': 'Left',
		'table-right': 'Right',
		'table-top': 'Top',
		'table-bottom': 'Bottom',
		'table-all': 'Left,Right,Top,Bottom',
		'sel-left': '', 'sel-right': '', 'sel-top': '', 'sel-bottom': '', 'sel-all' : ''
	};

	var button_sel = {
		'sel-left': 'Left',
		'sel-right': 'Right',
		'sel-top': 'Top',
		'sel-bottom': 'Bottom',
		'sel-all': 'Left,Right,Top,Bottom',
		'table-left': '', 'table-right': '', 'table-top': '', 'table-bottom': '', 'table-all' : ''
	};

	var f = new liwe.form.instance ( 'jconed-table', 'no-action' );
	f.descr ( '<b>Table</b>' );
	f.number ( { label: 'Cellpadding', name: 'table-padding', size: 3, value: table_padding, onkeyup: 'jconed.__table_context()', nonl: true } );
	f.number ( { label: 'Cellspacing', name: 'table-spacing', size: 3, value: table_spacing, onkeyup: 'jconed.__table_context()' } );
	f.number ( { label: 'Border size', name: 'table-border-size', size: 3, value: table_border_size, nonl: true } );
	f.number ( { label: 'Border color', name: 'table-border-color', size: 6, value: table_border_color, onclick: 'ColorPicker.show(this)' } );
	f.descr ( { label: 'Borders', text: String.formatDict ( buttons, button_table ) } );
	f.descr ( String.formatDict ( '<b>Selections</b> <i>(%(length)s)</i>', { length: selections.length } ) );
	f.number ( { label: 'Border size', name: 'border-size', size: 3, value: border_size, nonl: true } );
	f.number ( { label: 'Border color', name: 'border-color', size: 6, value: border_color, onclick: 'ColorPicker.show(this)' } );
	f.descr ( { label: 'Borders', text: String.formatDict ( buttons, button_sel ) } );
	jconed.toolbar.set_context ( f.get () );

} );

jconed.__table_context = function ( table_border, border )
{
	var sel = jconed.__table_get_selection ();
	if ( ! sel ) return;
	var td = sel [ 0 ], table = sel [ 1 ];
	var selections = jconed.editor.get_selection ();

	var f = liwe.form.get ( 'jconed-table' );

	var table_padding = f.get_value ( 'table-padding', '' );
	var table_spacing = f.get_value ( 'table-spacing', '' );
	var table_border_size = f.get_value ( 'table-border-size', '' );
	var table_border_color = f.get_value ( 'table-border-color', '' );
	var border_size = f.get_value ( 'border-size', '' );
	var border_color = f.get_value ( 'border-color', '' );

	function _borders ( el, border, size, color )
	{
		if ( typeof ( border ) != 'undefined' )
		{
			var borders = border.split ( ',' );
			var value = size + 'px solid ' + color;
			borders.iterate ( function ( b )
			{
				var prop = 'border' + b;
				if ( el.style [ prop ] != '' )
					value = '';
				el.style [ prop ] = value;
			} );
		}
	}

	if ( table_border )
	{
		_borders ( table, table_border, table_border_size, table_border_color );
		ColorPicker.hide ();
	}
	else if ( border )
	{
		if ( selections.length > 1 )
		{
			selections.iterate ( function ( sel )
			{
				var td = sel.startContainer.childNodes [ sel.startOffset ];
				_borders ( td, border, border_size, border_color );
			} );
		}
		else
		{
			_borders ( td, border, border_size, border_color );
		}
		ColorPicker.hide ();
	}

	table.cellPadding = table_padding;
	table.cellSpacing = table_spacing;
};
